package com.keste.adf.ui.bean;


import com.keste.adf.ui.bean.session.GlobalInfo;
import com.keste.adf.ui.fwk.util.ADFUtils;
import com.keste.adf.ui.fwk.util.QuizUtils;
import com.keste.adf.ui.login.SQLAuthenticatorAdapter;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.event.DialogEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Row;


public class UserLogin {
    private RichPopup userAlreadyExsists;
    private RichInputText userRegestrationId;
    boolean userPresents = false;
    boolean passwordmatch = false;
    boolean emailCheck=false;
    boolean genderCheck=false;
    boolean genderCheckOnValueChange=false;
    private RichInputText passWord;
    private RichInputText passWordConformed;
    private RichInputText emailAddress;
    private RichSelectOneChoice userGender;
    private RichSelectOneChoice userSelectedGender;
    private GlobalInfo globalInfo;
    
    private String currentUser;

    public UserLogin() {
        globalInfo = (GlobalInfo)QuizUtils.getGlobalInfo();
        
    }
    SQLAuthenticatorAdapter user = new SQLAuthenticatorAdapter();
/**
     *
     * @param actionEvent
     */
    public void userLogin(ActionEvent actionEvent) {
        // Add event code here...
//        String userId =
//            (String)ADFContext.getCurrent().getPageFlowScope().get("userId");
//        String userPassword =
//            (String)ADFContext.getCurrent().getPageFlowScope().get("userPw");
//        try {
//            user.connect();
//            user.doLogin(userId, userPassword);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } 
    }
    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }
    /**
     *
     * @param actionEvent
     */
    public void userSignUp(ActionEvent actionEvent) {
        // Add event code here...
         String usergender = (String)userSelectedGender.getValue();
         if(usergender==null)
         {
                genderCheck=true;
         }
        DCBindingContainer bindings = (DCBindingContainer)getBindings();
        DCIteratorBinding dcIter = bindings.findIteratorBinding("UsersVOIterator");
        Row row = dcIter.getCurrentRow();
        String FirstName = (String)row.getAttribute("FirstName");
        String SelectedGender = (String)row.getAttribute("GenderMf");
        System.out.println(SelectedGender);
        System.out.println(FirstName);
        
//        String userRegId =
//            (String)ADFContext.getCurrent().getPageFlowScope().get("userRegistarionId");
//        String userRegPw =
//            (String)ADFContext.getCurrent().getPageFlowScope().get("userRegistarionPassWord");
//        String userRegDet =
//            (String)ADFContext.getCurrent().getPageFlowScope().get("userRegistarionDetals");
//        try {
//            user.connect();
//            user.createUser(userRegId, userRegPw, userRegDet);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } 
         
     //    ADFUtils.invokeEL("#{bindings.Commit.execute}");
    }
    /**
     *
     * @param valueChangeEvent
     */
    public void userAlreadyExisting(ValueChangeEvent valueChangeEvent) {
        // Add event code here...
//        String userRegId = userRegestrationId.getValue().toString();
//        try {
//            user.connect();
//            if (userRegId != null) {
//                 boolean userexsists = user.userExists(userRegId);
//                if (userexsists) {
//                    userPresents = true;
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
    /**
     *
     * @param valueChangeEvent
     */
    public void passwordMatchChecker(ValueChangeEvent valueChangeEvent) {
        // Add event code here...
//        boolean isOn =
//            Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
//        System.out.println(isOn);
//        String password1 = passWord.getValue().toString();
//        String password2 = passWordConformed.getValue().toString();
//        if (!password1.equals(password2)) {
//            passwordmatch = true;
//        }
    }
    /**
     *
     * @param valueChangeEvent
     */
    public void emailValidator(ValueChangeEvent valueChangeEvent) {
       
        String mailId=emailAddress.getValue().toString();
        String emailreg="[ A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+[.]+[A-Za-z ]{2,10}";
        boolean regCheck=mailId.matches(emailreg);
        if(!regCheck)
        {
        emailCheck=true;
        }
    }
    /**
     *
     * @param valueChangeEvent
     */
    public void onGenderChange(ValueChangeEvent valueChangeEvent) {
        // Add event code here...
       
//           String gender = (String)valueChangeEvent.getNewValue();
//        if(gender==null)
//        {
//               genderCheck=true;
//        }
//        else
//        {
//                genderCheck=false;
//            }
    }
    public void setUserAlreadyExsists(RichPopup userAlreadyExsists) {
        this.userAlreadyExsists = userAlreadyExsists;
    }

    public RichPopup getUserAlreadyExsists() {
        return userAlreadyExsists;
    }



    public void setUserRegestrationId(RichInputText userRegestrationId) {
        this.userRegestrationId = userRegestrationId;
    }

    public RichInputText getUserRegestrationId() {
        return userRegestrationId;
    }

    public void setUserPresents(boolean userPresents) {
        this.userPresents = userPresents;
    }

    public boolean isUserPresents() {
        return userPresents;
    }

    public void setPassWord(RichInputText passWord) {
        this.passWord = passWord;
    }

    public RichInputText getPassWord() {
        return passWord;
    }

    public void setPassWordConformed(RichInputText passWordConformed) {
        this.passWordConformed = passWordConformed;
    }

    public RichInputText getPassWordConformed() {
        return passWordConformed;
    }

    public void setPasswordmatch(boolean passwordmatch) {
        this.passwordmatch = passwordmatch;
    }

    public boolean isPasswordmatch() {
        return passwordmatch;
    }


    public void setEmailCheck(boolean emailCheck) {
        this.emailCheck = emailCheck;
    }

    public boolean isEmailCheck() {
        return emailCheck;
    }


    public void setEmailAddress(RichInputText emailAddress) {
        this.emailAddress = emailAddress;
    }

    public RichInputText getEmailAddress() {
        return emailAddress;
    }


    public void setGenderCheck(boolean genderCheck) {
        this.genderCheck = genderCheck;
    }

    public boolean isGenderCheck() {
        return genderCheck;
    }

    public void setGenderCheckOnValueChange(boolean genderCheckOnValueChange) {
        this.genderCheckOnValueChange = genderCheckOnValueChange;
    }

    public boolean isGenderCheckOnValueChange() {
        return genderCheckOnValueChange;
    }

    public void setUserSelectedGender(RichSelectOneChoice userSelectedGender) {
        this.userSelectedGender = userSelectedGender;
    }

    public RichSelectOneChoice getUserSelectedGender() {
        return userSelectedGender;
    }

    public void onUserLogin(DialogEvent dialogEvent) {
        
        if(dialogEvent.getOutcome().equals(DialogEvent.Outcome.ok))
        {
            globalInfo.getDBConnector().doLogin((String)QuizUtils.getPageFlowScopeMap().get("userId"), (String)QuizUtils.getPageFlowScopeMap().get("userPw"));    
        }


        OperationBinding setCurrentUserMethod = ADFUtils.findOperationBinding("setCurrentUser");
        if (setCurrentUserMethod != null) {
            setCurrentUserMethod.getParamsMap().put("loginId", QuizUtils.getPageFlowScopeMap().get("userId"));
            setCurrentUserMethod.execute();
        }
        
        currentUser = (String)QuizUtils.getPageFlowScopeMap().get("userId");
        QuizUtils.getPageFlowScopeMap().put("userId","");
        QuizUtils.getPageFlowScopeMap().put("userPw","");
    }

    public void onUserLogout(DialogEvent dialogEvent) {
        if(dialogEvent.getOutcome().equals(DialogEvent.Outcome.ok))
        {
            globalInfo.getDBConnector().logout();
        }
        
        OperationBinding resetCurrentUserMethod = ADFUtils.findOperationBinding("resetCurrentUser");
        if (resetCurrentUserMethod != null) {
            resetCurrentUserMethod.execute();
        }
        
    }
}
